<?php
namespace app\controller;

use app\common\Controller;
use app\model\Comment;
use app\common\Model;
use app\common\DB;

class Index extends Controller
{
    /**
     * 添加评论
     */
    public function create()
    {
        $model = Comment::model();
        list($code, $msg, $data) = $model->add($_REQUEST);
        $data['sqls'] = $model->getAllSqls();
        return $this->outjson($code, $msg, $data);
    }
    
    /**
     * 展示评论列表
     */
    public function index()
    {
        //确定展示形式
        $postName = 'show_type';
        $sessionName = __METHOD__ . $postName;
        $model = Comment::model();
        $showTypes = $model->getShowTypes();
        if (isset($_POST[$postName]) && isset($showTypes[$_POST[$postName]]))
        {
            //前端选择切换
            $showType = $_POST[$postName];
            session($sessionName, $showType);
            return $this->redirect('/');
        }
        else
        {
            //尝试从session取
            $showType = session($sessionName);
            if (empty($showType) || !isset($showTypes[$showType]))
            {
                //session中取不到或数据错误
                $showType = $model->getDefaultShowType();
                session($sessionName, $showType);
            }
        }
        $commentItemsView = sprintf("index/comment-items-%s.php", $showType);
//         $commentItemsView = sprintf("index/static/comment-items-%s.php", $showType);
        $size = 10;
        $page = !empty($_GET['page']) && ctype_digit($_GET['page']) ? $_GET['page'] : 1;
        $offset = ($page - 1) * $size;
        echo view('index/list.php', [
            'commentItemsView' => $commentItemsView,
            'showType' => $showType,
            'showTypes' => $showTypes,
            'model' => $model,
            'page' => $page,
            'size' => 10,
            'offset' => $offset,
        ]);
    }
    
    public function vote()
    {
        $model = Comment::model();
        list($code, $msg, $data) = $model->updateVote($_REQUEST);
        $data['sqls'] = $model->getAllSqls();
        return $this->outjson($code, $msg, $data);
    }
    
    public function more()
    {
        if (empty($_POST['root_comment_id']) || !ctype_digit($_POST['root_comment_id']))
        {
            return $this->outjson(1, "lack of root_comment_id", $_POST);
        }
        if (empty($_POST['offset']) || !ctype_digit($_POST['offset']))
        {
            return $this->outjson(1, "lack of offset", $_POST);
        }
        $model = Comment::model();
        $comments = $model->getMore([
            'parent_comment_id' => $_POST['root_comment_id'],
            'offset' => $_POST['offset']
        ]);
        return $this->outjson(0, "OK", $comments);
    }
    
    function test()
    {
        $comment = Comment::model();
        dd($comment->select(['where' => 'id > :id', 'bind' => [':id' => 10]]), $comment->getLastSql());
    }
}